package com.muki.succ.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.muki.spider.Spider;
import com.muki.succ.core.Manager;
import com.muki.succ.web.service.ManagerService;
import com.muki.succ.web.service.SpiderService;

/**
 * 爬虫controller
 * @author ljh_2017
 *
 */
@RestController
public class SpiderController {

	@Autowired private ManagerService managerService;
	
	@Autowired private SpiderService spiderService;
	
	@RequestMapping(value="running")
	public List<Spider> getSpider() {
		return spiderService.runningSpiders();
	}
	
	@RequestMapping(value="start",method=RequestMethod.POST)
	public boolean startAndShutdown(@RequestParam("name")String name,@RequestParam("flag")boolean isstart) {
		try {
			if(isstart)
				spiderService.startSpider(name);
			else
				spiderService.shutDownSpider(name);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	@RequestMapping(value="registery",method=RequestMethod.GET)
	public String createSpider(@RequestParam("theme")String topic) {
		spiderService.createSpider(topic);
		return "创建爬虫:"+topic;
	}
	
	@RequestMapping(value="managers")
	public List<Manager> getManagers(){
		return managerService.getManagerNames();
	}
}
